package zongbao.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import zongbao.domain.Book;
import zongbao.domain.User;
import zongbao.service.BookService;
import zongbao.service.UserService;


import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;


@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    User login=null;
    User zhaohui=null;
    //登录
    @RequestMapping(value = "/login",produces = "text/html;charset=UTF-8")
    public String login(User user, Model model, HttpSession session){
      login = userService.login(user);
      if (login!=null){
          session.setAttribute("USER_SESSION",login);
            return "main";
        }else {
          model.addAttribute("msg","错误，重新登录");
          return "login";
        }
    }
    //返回首页
    @RequestMapping("/main")
    public String main(){
        return "main";
    }
    //忘记密码
    @RequestMapping("/wm")
    public ModelAndView wm(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("user-wm");
        return modelAndView;
    }
    @RequestMapping("/zhaohui")
    public String zhaohui(User user, Model model){
        zhaohui = userService.zhaohui(user);
        if (zhaohui!=null){
            return "user-zhaohui";
        }else {
            model.addAttribute("msg","信息有误，请重新输入");
            return "redirect:/user/wm";
        }
    }
    @RequestMapping("/dao")
    public String xiugai(User user, Model model){
            User user1 = new User();
            user1.setPassword(user.getPassword());
            user1.setId(zhaohui.getId());
            userService.updatepwd(user1);
            model.addAttribute("msg","重置密码成功，请重新登录");
            return "login";
        }
    //根据学生id查找所借图书
    @Autowired
    private BookService bookService;
    ///book?id=1
    @RequestMapping("/book")
    public ModelAndView findBookById(){
        List<Book> list = bookService.findUserById(login.getId());
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("user-book");
        modelAndView.addObject("list",list);
        return modelAndView;
    }
    //根据学生id查找学生信息
    @RequestMapping("/a")
    public ModelAndView findUserById(){
        User user = userService.findUserById(login.getId());
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("user");
        modelAndView.addObject("user",user);
        return modelAndView;
    }
    //查看所有可借图书
    @RequestMapping("/allbook")
    public ModelAndView allbook(){
        List<Book> books = bookService.allBook();
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("book");
        modelAndView.addObject("list",books);
        return modelAndView;
    }
    //借书
    @RequestMapping("/jiebook/{bookId}")
    public String jiebook(@PathVariable("bookId") Integer id){
        User user=new User();
        user.setId(login.getId());
        User userById = userService.findUserById(login.getId());
        user.setJiebook(userById.getJiebook()+1);
        userService.jieshu(user);
        Book book = new Book();
        book.setId(id);
        book.setUserid(login.getId());
        book.setUsertime(new Date());
        book.setGuihuan("已被借出");
         bookService.jieBook(book);
        return "redirect:/user/allbook";
    }
    //还书
    @RequestMapping("/huanbook/{bookId}")
    public String huanbook(@PathVariable("bookId") Integer id){
        User user=new User();
        user.setId(login.getId());
        User userById = userService.findUserById(login.getId());
        user.setJiebook(userById.getJiebook()-1);
        userService.jieshu(user);
        Book book = new Book();
        book.setId(id);
        book.setBooktime(new Date());
        book.setGuihuan("已归还,待管理员确定");
        bookService.huanBook(book);
        return "redirect:/user/book";
    }
    //注册
    @RequestMapping("/inadd")
    public ModelAndView inUser() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("user-inadd");
        return modelAndView;
    }
    @RequestMapping("/add")
    public String add(User user,Model model){
        userService.adduser(user);
        model.addAttribute("msg","注册成功，请重新登录");
        return "login";
    }
    //修改密码
    @RequestMapping("/pwd")
    public ModelAndView pwd() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("user-pwd");
        return modelAndView;
    }
    @RequestMapping("/pwod")
    public String pwod(@RequestParam("pwd") String pwd,User user, Model model){
        if (user.getPassword().equals(login.getPassword())){
            User user1 = new User();
            user1.setPassword(pwd);
            user1.setId(login.getId());
        userService.updatepwd(user1);
        model.addAttribute("msg","密码修改成功，请重新登录");
        return "login";
        }else {
            model.addAttribute("msg","旧密码错误，请重新输入");
            return "redirect:/user/pwd";
        }
    }
    //退出登录
    @RequestMapping("/logout")
    public String logout(HttpSession session){
        //清除Session
        session.invalidate();
        //退出登录后重定向到登录页面
        return "login";
    }
}
